<template>
  <div class="login-container">
    <el-form
      ref="form"
      :model="form"
      :rules="rules"
      label-width="80px"
      class="login-form"
    >
      <h2 class="login-title">WeBlog后台管理</h2>
      <el-form-item label="账户" prop="username">
        <el-input v-model="form.username"></el-input>
      </el-form-item>

      <el-form-item label="密码" prop="password">
        <el-input type="password" v-model="form.password"></el-input>
      </el-form-item>
      <el-checkbox v-model="checked" class="rememberme" auto
        >记住密码</el-checkbox
      >

      <el-form-item>
        <el-button type="primary" @click="submitForm()">登录</el-button>
      </el-form-item>
    </el-form>
  </div>
</template>

<script>
import { login, getUserInfo } from "@/api/login";
export default {
  data() {
    return {
      checked: false,
      form: {
        username: "",
        password: ""
      },
      rules: {
        username: [
          { required: true, message: "账号不能为空", trigger: "blur" }
        ],
        password: [{ required: true, message: "密码不能为空", trigger: "blur" }]
      }
    };
  },
  methods: {
    submitForm() {
      this.$refs.form.validate(valid => {
        if (valid) {
          this.$store
            .dispatch("Login", this.form)
            .then(resp => {
              if (resp.flag) {
                this.$router.push("/");
              } else {
                this.$message({
                  type: "warning",
                  message: resp.message
                });
              }
            })
            .catch(error => {});
          /* login(this.form.username, this.form.password).then(resp => {
            const respData = resp.data;
            if (respData.flag) {
              localStorage.setItem("wblog-user-token", respData.data.token);
              localStorage.setItem("wblog-user-role", respData.data.role);

              //验证成功,通过token获取用户信息
              getUserInfo(respData.data.token).then(resp2 => {
                if (resp2.data.flag) {
                  localStorage.setItem(
                    //存入user信息
                    "wblog-user-data",
                    JSON.stringify(resp2.data.data)
                  );
                  this.$router.push("/");
                } else {
                  this.$message({
                    message: resp2.data.message,
                    type: "warning"
                  });
                }
              });
            } else {
              this.$message({
                message: respData.message,
                type: "error"
              });
            }
          });*/
        } else {
          //检验不合格
          this.$message({
            message: "用户名或密码不合法",
            type: "warning"
          });
          return false;
        }
      });
    }
  }
};
</script>

<style scoped>
.login-form {
  width: 350px;
  background-color: rgb(255, 255, 255, 0.8);
  margin: 140px auto;
  padding: 30px;
  border-radius: 20px;
}
.login-container {
  position: absolute;
  width: 100%;
  height: 120%;
  background: url("../../assets/bg1.jpg");
  background-repeat: no-repeat;
  background-size: cover;
  margin: 0px 0px;
}
.login-title {
  color: #303133;
  text-align: center;
}
</style>
